<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="com.sz.ticket.dao.ITicketDAO" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String style1 = "width: 150px;text-align:right;font-size: 12px;";
String style2 = "text-align:left;width: 294px";

ServletContext servletContext = request.getSession().getServletContext(); 
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ITicketDAO dao = (ITicketDAO) context.getBean("TicketDAO");
List list = dao.queryTicketStatusList();

String usertype = request.getSession().getAttribute("usertype").toString();
String regionid = request.getSession().getAttribute("regionid").toString();
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	    <link href="<%= request.getContextPath() %>/css/main.css" rel="stylesheet" type="text/css" />
		<base href="<%=basePath%>">

		<title>查询订单</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="addTicket">
		<meta http-equiv="description" content="查询订单">
		
		<style type="text/css"> 
			@import "js/tableview/TableView.css";
			
	
    	</style>
	</head>
	
	<body>
		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/tableview/SortView.js"></script>
		<script type="text/javascript" src="js/tableview/PagerView.js"></script>
		<script type="text/javascript" src="js/tableview/TableView.js"></script>
		<script type="text/javascript" src="js/tableview/SelectorView.js"></script>
		<script type="text/javascript" src="js/tableview/PaperView.js"></script>
		<script type="text/javascript" src="js/utility.js"></script>
		<script type="text/javascript" src="js/calendar.js"></script>
		<table width="100%" height="50px" border="0" cellpadding="0" cellspacing="0">
	  	 
	  	   		<tr>
		  	<td class="title0">订单查询变更</td>
		  	
	  	</tr>
	  	</table>
		<table width="100%" height=120px border="0" cellpadding="0" cellspacing="0">
			<tr>
			  	<td style="<%=style1%>">商家编码&nbsp;</td>
			  	<td>
			  		<input  type="text" id="business_id" name="business_id"  style="<%=style2%>">
			  	</td>
			  	
			  	<td style="<%=style1%>">商家名称&nbsp;</td>
			  	<td>
			  		<input  type="text" id="business_name" name="business_name"  style="<%=style2%>">
			  	</td>				  	
		  	</tr>	

	  		<tr>
	  			<td style="<%=style1%>">客户电话&nbsp;</td>
			  	<td>
			  		<input  type="text" id="cust_tel" name="cust_tel"  style="<%=style2%>">
			  	</td>	
			  	<td style="<%=style1%>">客户姓名&nbsp;</td>
			  	<td>
			  		<input type="text" id="cust_name" name="cust_name" style="<%=style2%>">
			  	</td>			  	
		  	</tr>
		  	
		  	<tr>
		  		<td style="<%=style1%>">所处岗位&nbsp;</td>
			  	<td>
					<select id="in_managertype" name="in_managertype" style="width: 294px;">
					   <option value="" style="font-style: italic;" >------所有岗位------</option>
					   <option value="2" style="font-style: italic;" <%if(usertype.equals("2")){%>selected="selected" <%}%> >前台</option>
					   <option value="3" style="font-style: italic;" <%if(usertype.equals("3")){%>selected="selected" <%}%> >中台</option>				   				   
					</select>
			  	</td>
			  	
		  		<td style="<%=style1%>">订单状态&nbsp;</td>
			  	<td>
					<select id="status" name="status" style="width: 294px;" onchange="onSelectChange_Status(this.value)">
					   <option value="" selected="selected" style="font-style: italic;" >------所有状态------</option>
					   <%
					   String value = null;
					   String text 	= null;
					   boolean b 	= false;
					   for(int n = 0; n < list.size(); ++n) {						   
						   Map map = (Map)list.get(n);
						   value = map.get("STATUS_ID").toString();
						   text = map.get("DESCRIP").toString();
						   //b = value.equals("1");  //默认选择
					   %>
					   <option <%if(b){%> selected="selected" <%}%>
					     value="<%=value%>"><%=text%>
					   </option>
					   <%}%>				   
					</select>
			  	</td>
	  		</tr>
	  		
	  		<tr>
		  		<td style="<%=style1%>">下单时间</td>
			  	<td>
			  		<input type="text" id="create_time_d_begin" name="create_time_d_begin" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="create_time_t_begin" name="create_time_t_begin" style="width: 56px; display: none" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>
			  		-
			  		<input type="text" id="create_time_d_end" name="create_time_d_end" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="create_time_t_end" name="create_time_t_end" style="width: 56px; display: none" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>			  		
			  	</td>
			  	<td style="<%=style1%>">预约时间</td>
			  	<td>
			  		<input type="text" id="demand_time_d_begin" name="demand_time_d_begin" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="demand_time_t_begin" name="demand_time_t_begin" style="width: 56px; display: none" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>
			  		-
			  		<input type="text" id="demand_time_d_end" name="demand_time_d_end" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="demand_time_t_end" name="demand_time_t_end" style="width: 56px; display: none" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>			  		
			  	</td>
	  		</tr>
	  		
	  	    <tr>
		  		<td style="<%=style1%>">是否锁定&nbsp;</td>
		  		<td style="<%=style2%>">
		  			<select id="lock" name="lock" >
					   <option value="" style="font-style: italic;" >------全部订单------</option>
					   <option value="0" style="font-style: italic;"  selected="selected" >未锁定</option>
					   <option value="1" style="font-style: italic;" >已锁定</option>					  					  
					</select>		  			
		  		</td>
		  		
		  		<td class="title0">
            		<input type="button" id="btn" name="btn" value="查询" onclick="onClickQuery()">
	  			</td>			  	
		 	</tr>
	  	</table>

	  	<div id="table_div" style= "width: 100%"></div>
		<div id="pager"></div>
		
	  	<table width="100%">
	  		<tr align="center">
		  		<td colspan="2" class="title0">		
					<input type="button" id="btn" name="btn" value="预占" onclick="onClickLock()">
	  				<input type="button" id="btn" name="btn" value="编辑" onclick="onClickEdit()">
					<input type="button" id="btn_custom_cancel" name="btn_custom_cancel" value="撤单" onclick="onClickCancel('1')"> <!-- 客户撤单 -->
 					<input style="display:none" type="button" id="btn_business_cancel" name="btn_business_cancel" value="商家撤单" onclick="onClickCancel('2')">
	  			</td>		  	
  			</tr>  
	  	</table>
	  	

		
		<script type="text/javascript">
		
		var numPerPage = 10; //每页行数
		var paramValue = ""; //形如  "{STATUS:'1', CREATE_MANAGER:'1'}"
		var methodCount = "queryTicketCount";
		var methodPage = "queryTicketPage";
		
		var table = new TableView('table_div');
		table.header = {
			TICKET_ID			: '订单编号',
			STATUS_NAME			: '订单状态',
			CUST_TEL			: '客户电话',
			CUST_NAME			: '客户姓名',
			BUSINESS_NAME		: '商家名称',
			DEMAND_TIME_STR		: '预约时间',
			LOCK_MANAGER_NAME	: '预占人',
			CREATE_MANAGER_NAME	: '下单人',
			CREATE_TIME_STR		: '下单时间',
			CONFIRM_B_STR		: '商家确认',
			CONFIRM_C_STR		: '客户确认',
			SMS					: '短信',
			DETAIL				: '详细信息',
			LOG					: '日志'		
		};		
		table.customDefine = {
				TICKET_ID			: 'width = "60px"',
				STATUS_NAME			: 'width = "120px"',
				CUST_TEL			: 'width = "100px"',
				CUST_NAME			: 'width = "100px"',
				BUSINESS_NAME		: 'width = "120px"',
				DEMAND_TIME_STR		: '',
				LOCK_MANAGER_NAME	: '',
				CREATE_MANAGER_NAME	: '',
				CREATE_TIME_STR		: '',
				CONFIRM_B_STR		: '',
				CONFIRM_C_STR		: '',
				SMS					: 'width = "40px"',
				DETAIL				: '',
				LOG					: ''			
		};
				
		table.dataKey = 'TICKET_ID';
		table.count = false;
		table.title = false;
		table.display.filter = false;
		table.display.pager = false;
		table.pager.size = numPerPage;

		var pager = new PagerView('pager');
		pager.onclick = onPagerClick;
		pager.itemCount = 0;
		pager.size = numPerPage;
		
		table.render();
		pager.render();

		function queryTicket(pageNO, returnCount) {
  	  		var begin = (pageNO-1) * numPerPage + 1;
  	  		var end = pageNO * numPerPage;
  	  		var mc = returnCount==true ? methodCount : "";
  	  		var date = {
  	  			BEAN_METHOD 	: "TicketBMO.queryTicket",
  	  			PARAM_CLASS 	: "java.util.Map",
  	    	  	PARAM_VALUE 	: paramValue,
  	    	  	METHOD_COUNT 	: mc, //为空则不返回记录数
  	    	  	METHOD_PAGE 	: methodPage, //为空则不分页
  	    	  	PAGE_BEGIN 		: begin, //仅在METHOD_PAGE不为空时使用
  	    	  	PAGE_END 		: end
  	  	    };
  	  		$.ajax({
				type: "POST",
				url: "query.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: query_success,
				error: show_error
  	  		});

  	  		/*
  			var ids = table.getSelectedKeys();
  			table.pager.size = 6;
  			
  			table.clear();
  			table.add({id: 2, name: 'kao2', name_cn: '杰瑞', text: 'Jerrimy'}); 
  			table.add({id: 3, name: 'kao3', name_cn: '杰瑞', text: 'Jerrimy'}); 
			var range = new Array(5);
			range.push({id: 40, name: 'kao', name_cn: '幽灵', text: 'None'});
			range.push({id: 60, name: 'kao', name_cn: '幽灵', text: 'None'});
			table.addRange(range);*/
  			return true;
  			
  		}
  		function onSelectChange_Status(value) {
  			if(value != "") { //指定了某一状态
  				document.getElementById("in_managertype").value = "";
  				document.getElementById("lock").value = "";
  			}
  		}
  		function onPagerClick(pageNO) {
  			queryTicket(pageNO, "FLASE");
  		}
  		
  		//刷新当前页
  		function refreshCurrentPage() {
  			queryTicket(pager.index, "FLASE");
  		}

  		function query_success(data) {
  	  		var jdata = eval("(" + data + ")");
  	  		if(jdata.COUNT != null) { //有记录数返回  	  	  		
  	  	  		pager.itemCount = jdata.COUNT;
  	  	  		pager.size = numPerPage;  	  	  		
  	  	  		pager.render();
  	  		} 
		   var array = eval(jdata.LIST);
		   for(var i = 0; i < array.length; ++i) {
		   		//形如：<a href="javascript:" onclick="about()">modal</a>
		   		var url = "<a href=\"javascript:\" onclick=\"onTicketDetail('";
		   		url += array[i].TICKET_ID;
		   		url += "')\">详细信息</a>";
		   		array[i].DETAIL = url;
		   		
		   		if(array[i].CONFIRM_B == "1") {
		   			array[i].CONFIRM_B_STR = "已确认";
		   		} else if(array[i].CONFIRM_B == "-1"){
		   			array[i].CONFIRM_B_STR = "确认失败";
		   		} else {
		   			array[i].CONFIRM_B_STR = "待确认";
		   		}
		   		if(array[i].CONFIRM_C == "1") {
		   			array[i].CONFIRM_C_STR = "已确认";
		   		} else if(array[i].CONFIRM_C == "-1"){
		   			array[i].CONFIRM_C_STR = "确认失败";
		   		} else {
		   			array[i].CONFIRM_C_STR = "待确认";
		   		}
		   		
		   		//短信
		   		url = "<input type=\"button\" value=\"短信\" onclick=\"onSMS('";
		   		url += array[i].TICKET_ID;
		   		url += "','";
		   		url += array[i].CUST_TEL		   		
		   		url += "')\" />";
		   		array[i].SMS = url;
		   		
		   		//日志
		   		url = "<a href=\"javascript:\" onclick=\"onViewLog('";
		   		url += array[i].TICKET_ID;		   		  		
		   		url += "')\">日志</a>";
		   		array[i].LOG = url;
		   }
		   table.clear();
		   table.addRange(array);
		}
  		function show_error(data) {
  			var err = "操作失败！\n";
  			err += "status：" + data.status + "\n";
  			err += "statusText：" + data.statusText;
  	  		alert(err);
 		}
  		function onClickQuery() {
  	  		
  			var tmp;
			paramValue = "{";			
			
			//region
  			paramValue += " REGION_ID:'" + "<%=regionid%>";
			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("cust_tel").value;
  			paramValue += " CUST_TEL:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("cust_name").value;
  			paramValue += " CUST_NAME:'" + tmp;
  			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("status").value;
  			paramValue += " STATUS:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_id").value;
  			paramValue += " BUSINESS_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_name").value;
  			paramValue += " BUSINESS_NAME:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			//下单时间
			tmp = document.getElementById("create_time_d_begin").value;
			/*if(tmp != null && tmp != "") {
				var t = document.getElementById("create_time_t_begin").value;
				if(t == null || t == "") t="0"; //开始时间默认最小0
				
				tmp += " "+t;
			}*/  //屏蔽 只按天查
			paramValue += " CREATE_TIME_BEGIN:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("create_time_d_end").value;
			/*if(tmp != null && tmp != "") {
				var t = document.getElementById("create_time_t_end").value;
				if(t == null || t == "") t="23"; //结束时间默认最大23
				
				tmp += " "+t;
			}*/  //屏蔽 只按天查
			paramValue += " CREATE_TIME_END:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			//预约时间
			tmp = document.getElementById("demand_time_d_begin").value;
			/*if(tmp != null && tmp != "") {
				var t = document.getElementById("demand_time_t_begin").value;
				if(t == null || t == "") t="0"; //开始时间默认最小0
				
				tmp += " "+t;
			}*/
			paramValue += " DEMAND_TIME_BEGIN:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("demand_time_d_end").value;
			/*if(tmp != null && tmp != "") {
				var t = document.getElementById("demand_time_t_end").value;
				if(t == null || t == "") t="23"; //结束时间默认最大23
				
				tmp += " "+t;
			}*/
			paramValue += " DEMAND_TIME_END:'" + tmp;
			paramValue += "'";
			paramValue += ",";	
			
			paramValue += " __ORDERBY:'order by t.create_time desc',";
	
			
			
  			tmp = document.getElementById("lock").value;
  			if(tmp == "0") {
	  			paramValue += " __ADDITION:' t.lock_manager is null'";							
  			} else if(tmp == "1") {
  				paramValue += " __ADDITION:' t.lock_manager is not null'";	
  			} else {
  				paramValue += " __ADDITION:''";
  			}
  			  			
  			paramValue += "}";
  				
  	  		pager.index = 1;
  	  		queryTicket(1, true);
  		}
  		function onTicketDetail(ticket_id) {
   			var url = "<%=path%>";
  			url += "/page/viewTicket.jsp?ticket_id=" + ticket_id;
  			var param = "status:no;scroll:no;dialogWidth:1024px;dialogHeight:600px";
			//window.showModalDialog(url, window, param);
			
			var width = 1024; //弹出窗口的宽度;
			var height = 600; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"订单信息","height="+height+",width="+width+",top="+top+",left="+left+","+style); 			
  		}
  		
  		function onClickLock() {
  			//var ids = table.getSelectedKeys(); // Array[Key]
  			var rows = table.getSelected(); // Array[Object]
  			if(rows.length == 0) {
  				alert("请选择订单");
  				return;
  			}else if(rows.length > 5) {
  				alert("对不起，您最多只能锁定 5 条订单");
  				return;
  			}
			var str = "[";
			for(var i = 0; i < rows.length; ++i) {
				str += "{";
				str += "TICKET_ID:";
				str += "'" + rows[i].TICKET_ID + "'";
				str += ",MYVERSION:";
				str += "'" + rows[i].VERSION_STR + "'";
				str += ",LOCKFLAG:";
				str += "'" + 1 + "'";
				str += "}"
				if(i < rows.length-1) str +=",";
			}	
  			str += "]";
  			
  			$.ajax({
				type: "POST",
				url: "ticket/lockTicket.ajax",
				//data: {id:'123'},
				data: {TICKETS:str, OPERATE_TYPE:"LOCK"},
				datatype:"json",
				success: lockSuccess,
				error: show_error
  	  		});
  			
  		}
 		function showMsg(data) {
  	  		alert(data);
 		}
 		function lockSuccess(data) {
  	  		alert(data);
  	  		onClickQuery();  	  		
 		}
 		
 		function onClickEdit() {
 			var rows = table.getSelected(); // Array[Object]
  			if(rows.length != 1) {
  				alert("请选择1 条订单");
  				return;
  			}
  			
  			url = "<%=path%>";
  			url += "/ticket/editTicket.do?ticket_id=" + rows[0].TICKET_ID;
  			url += "&title=" + encodeURI(encodeURI("编辑订单"));
  			url += "&cur_version=" + rows[0].VERSION_STR ;
  			url += "&final_callback=refreshCurrentPage()"; //按目前的逻辑，确认成功后会转到“操作成功”的界面，在“操作成功”的界面回调这个函数刷新
  			var param = "status:no;scroll:no;dialogWidth:1024px;dialogHeight:700px";
			//window.showModalDialog(url, window, param);
			//refreshCurrentPage();
			
			var width = 1000; //弹出窗口的宽度;
			var height = 700; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"编辑订单","height="+height+",width="+width+",top="+top+",left="+left+","+style);
			
 		}
 		
 		function onClickCancel(from) {
 			var from_str = "";
 			if(from == "1") from_str="客户";
 			else if(from == "2") from_str="商家";
 			
 			if(from_str == "") return;
 			
 			if(window.confirm("确认"+from_str+"撤单吗?") == false) return;
 			
 			var rows = table.getSelected(); // Array[Object]
  			if(rows.length != 1) {
  				alert("请选择1条订单");
  				return;
  			}
  			
			var param = "{TICKET_ID:";
  			param += "'"+rows[0].TICKET_ID+"',";
  			param += "CANCEL_FROM:";
  			param += "'"+from+"',";
  			param += "MYVERSION:";
  			param += "'"+rows[0].VERSION_STR+"',";
  			
  			param += "}";
  			
  			$.ajax({
				type: "POST",
				url: "ticket/cancelTicket.ajax",
				//data: {id:'123'},
				data: {PARAM:param, OPERATE_TYPE:"CANCEL", RETURNTYPE:'MESSAGE'},
				datatype:"json",
				success: lockSuccess,
				error: show_error
  	  		});		
 		}
 		
 		function onSMS(ticket_id, tel) { 			
 			var url = "<%=path%>";
  			url += "/page/ticketSMS.jsp?ticket_id=" + ticket_id;
  			url += "&telno=" + tel;
  			url += "&type=1"; //默认是1 预定成功
  			
  			var width = 460; //弹出窗口的宽度;
			var height = 320; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"短信","height="+height+",width="+width+",top="+top+",left="+left+","+style); 			
 		}
 		
 		function onViewLog(ticket_id){
 		  	var url = "<%=path%>";
  			url += "/page/viewTicketLog.jsp?ticket_id=" + ticket_id;  		
  			
  			var width = 1024; //弹出窗口的宽度;
			var height = 768; //弹出窗口的高度;
			var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
			var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
			var style = "toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no";			
			window.open (url,"订单日志","height="+height+",width="+width+",top="+top+",left="+left+","+style); 			

 		}
	</script>
	
	</body>
</html>
